﻿using InChatter.Client.Service;
using InChatter.Client.WPFClient.Utility;
using InChatter.Client.WPFClient.View;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace InChatter.Client.WPFClient.ViewModel
{
    public class MessageControlViewModel:INotifyPropertyChanged
    {
        public DelegateCommand cmd_ShowHistoryWindow { get; private set; }
        public DelegateCommand cmd_ShowMessageWindow { get; private set; }

        private int _Count;

        public int Count
        {
            get { return _Count; }
            set
            {
                _Count = value;
                NotifyPropertyChanged("Count");
            }
        }


        public MessageControlViewModel()
        {
            Count = 0;
            cmd_ShowHistoryWindow = new DelegateCommand(() =>
            {
                ShowWinodw(MainWindow.Instance);
            });
            cmd_ShowMessageWindow = new DelegateCommand(() =>
            {
                MessagePoolWindow.Instance.ExpandMessageOrClose(true);
            });
            //注册监听数据事件
            ChatMsgCenter.Instance.MessageCountChanged += new Action<int>(Instance_MessageCountChanged);
        }

        private void Instance_MessageCountChanged(int count)
        {
            Count = count;
        }

        private void ShowWinodw(Window win)
        {
            if (win.Visibility != Visibility.Visible)
            {
                win.Show();
            }
            else
            {
                if (win.WindowState == WindowState.Minimized)
                {
                    win.WindowState = WindowState.Normal;
                }
                else
                {
                    win.Activate();
                }
            }
        }

        public void Login()
        {
            ChatMsgCenter.Instance.Login();
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
